﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.ViewModels/VisionOffsetViewModel
* 类       名: VisionOffsetViewModel
* 功      能:  TD
* 时      间:  2023/9/17 13:59:47
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using OfficeOpenXml.FormulaParsing.Excel.Operators;
using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.DAL.DataEntity;
using SiliconSelect.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace SiliconSelect.ViewModels
{
    public class VisionOffsetViewModel : NotifyBase
    {
        public VisionOffsetViewModel()
        {
            VisionOffsetList = new ObservableCollection<VisionOffsetModel> { new VisionOffsetModel() };
            VisionOffsetListRefresh();
            SaveVisionOffset = new Command(DoSaveVisionOffsetTask);
        }


        /// <summary>
        /// 将数据保存到数据库,并刷新和加载到全局变量中去.
        /// </summary>
        /// <param name="obj"></param>
        private async void DoSaveVisionOffsetTask(object obj)
        {
            string queryField;
            string queryVal;
            string[] updateFields = new string[2];
            double[] updateVals = new double[2];
            DataResult res;
            // 更新数据到数据库
            foreach (var visionOffset in VisionOffsetList)
            {
                queryField = "OptionName";
                queryVal = visionOffset.OptionName!;
                updateFields[0] = "OffsetK";
                updateFields[1] = "OffsetB";
                updateVals[0] = visionOffset.OffsetK;
                updateVals[1] = visionOffset.OffsetB;
                res = LocalDataAccess.UpdateVisionOffset(queryField, queryVal, updateFields.ToList(), updateVals.ToList());
            }
            // 刷线数据库
            VisionOffsetListRefresh();
            OperateRes = "保存成功!";
            OprateResShow = "Visible";

            await Task.Run(async () =>
            {
                await Task.Delay(1500);
            });
            OprateResShow = "Collapsed";
            OperateRes = "";
        }

        /// <summary>
        /// 刷新数据内容
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void VisionOffsetListRefresh()
        {
            DataResult<List<VisionOffsetEntity>> result = LocalDataAccess.GetVisionOffsetData();
            if (result.State && result.Data!.Count > 0)
            {
                VisionOffsetList.Clear();
                foreach (var entity in result.Data!)
                {
                    VisionOffsetModel model = new VisionOffsetModel();
                    model.Id = entity.Id;
                    model.ModuleName = entity.ModuleName;
                    model.OptionName = entity.OptionName;
                    model.OffsetK = entity.OffsetK;
                    model.OffsetB = entity.OffsetB;
                    VisionOffsetList.Add(model);
                    UpdateVisionOffsetGlobalMap(model.OptionName, model.OffsetK, model.OffsetB);
                }
            }
        }

        public void UpdateVisionOffsetGlobalMap(string? optionName, double offsetK, double offsetB)
        {
            double[] doubleKb = new double[] { offsetK, offsetB };
            switch (optionName)
            {
                case "平均厚度":
                    GlobalValues.VisionOffsetMap["ThicknessAver"] = doubleKb;
                    break;
                case "TTV":
                    GlobalValues.VisionOffsetMap["TTV"] = doubleKb;
                    break;
                case "线痕最大值":
                    GlobalValues.VisionOffsetMap["LineTraceMax"] = doubleKb;
                    break;
                case "翘曲":
                    GlobalValues.VisionOffsetMap["Warping"] = doubleKb;
                    break;
                case "最小厚度":
                    GlobalValues.VisionOffsetMap["ThicknessMin"] = doubleKb;
                    break;
                case "最大厚度":
                    GlobalValues.VisionOffsetMap["ThicknessMax"] = doubleKb;
                    break;
                case "是否密集线痕":
                    GlobalValues.VisionOffsetMap["IsDenseLine"] = doubleKb;
                    break;
                case "上线痕数量":
                    GlobalValues.VisionOffsetMap["LineTraceUpNum"] = doubleKb;
                    break;
                case "上线痕数量最大值":
                    GlobalValues.VisionOffsetMap["LineTraceUpMax"] = doubleKb;
                    break;
                case "下线痕数量":
                    GlobalValues.VisionOffsetMap["LineTraceDownNum"] = doubleKb;
                    break;
                case "下线痕数量最大值":
                    GlobalValues.VisionOffsetMap["LineTraceDownMax"] = doubleKb;
                    break;
                case "线痕数量":
                    GlobalValues.VisionOffsetMap["LineTraceCount"] = doubleKb;
                    break;
                case "边长最大值":
                    GlobalValues.VisionOffsetMap["LengthMax"] = doubleKb;
                    break;
                case "边长最小值":
                    GlobalValues.VisionOffsetMap["LengthMin"] = doubleKb;
                    break; ;
                case "X边长":
                    GlobalValues.VisionOffsetMap["LengthX"] = doubleKb;
                    break;
                case "Y边长":
                    GlobalValues.VisionOffsetMap["LengthY"] = doubleKb;
                    break;
                case "对角线左上右下":
                    GlobalValues.VisionOffsetMap["DiagonalLeftStart"] = doubleKb;
                    break;
                case "对角线右上左下":
                    GlobalValues.VisionOffsetMap["DialonalRightStart"] = doubleKb;
                    break;
                case "倒角最大值":
                    GlobalValues.VisionOffsetMap["ChamferMax"] = doubleKb;
                    break;
                case "倒角最小值":
                    GlobalValues.VisionOffsetMap["ChamferMin"] = doubleKb;
                    break;
                case "上下平行度":
                    GlobalValues.VisionOffsetMap["UpDownParallel"] = doubleKb;
                    break;
                case "左右平行度":
                    GlobalValues.VisionOffsetMap["LeftRightParallel"] = doubleKb;
                    break;
                case "直角边最大值":
                    GlobalValues.VisionOffsetMap["SquareLineMax"] = doubleKb;
                    break;
                case "直角边最小值":
                    GlobalValues.VisionOffsetMap["SquareLineMin"] = doubleKb;
                    break;
                case "垂直度最大值":
                    GlobalValues.VisionOffsetMap["VerticalAngleMax"] = doubleKb;
                    break;
                case "垂直度最小值":
                    GlobalValues.VisionOffsetMap["VerticalAngleMin"] = doubleKb;
                    break;
                case "左上倒角":
                    GlobalValues.VisionOffsetMap["LeftTopCharmfer"] = doubleKb;
                    break;
                case "右上倒角":
                    GlobalValues.VisionOffsetMap["RightTopCharmfer"] = doubleKb;
                    break;
                case "左下倒角":
                    GlobalValues.VisionOffsetMap["LeftBottomCharmfer"] = doubleKb;
                    break;
                case "右下倒角":
                    GlobalValues.VisionOffsetMap["RightBottomCharmfer"] = doubleKb;
                    break;
                case "左上角度":
                    GlobalValues.VisionOffsetMap["LeftTopAngle"] = doubleKb;
                    break;
                case "右上角度":
                    GlobalValues.VisionOffsetMap["RightTopAngle"] = doubleKb;
                    break;
                case "左下角度":
                    GlobalValues.VisionOffsetMap["LeftBottomAngle"] = doubleKb;
                    break;
                case "右下角度":
                    GlobalValues.VisionOffsetMap["RightBottomAngle"] = doubleKb;
                    break;
                case "X边长最大值":
                    GlobalValues.VisionOffsetMap["LengthXMax"] = doubleKb;
                    break;
                case "X边长最小值":
                    GlobalValues.VisionOffsetMap["LengthXMin"] = doubleKb;
                    break;
                case "Y边长最大值":
                    GlobalValues.VisionOffsetMap["LengthYMax"] = doubleKb;
                    break;
                case "Y边长最小值":
                    GlobalValues.VisionOffsetMap["LengthYMin"] = doubleKb;
                    break;
                case "电阻率值":
                    GlobalValues.VisionOffsetMap["NaponsNc"] = doubleKb;
                    break;
            }
        }

        private ObservableCollection<VisionOffsetModel> _visionOffsetList = null!;

        public ObservableCollection<VisionOffsetModel> VisionOffsetList
        {
            get { return _visionOffsetList; }
            set { SetProperty(ref _visionOffsetList, value); }
        }

        public Command SaveVisionOffset { get; set; }

        /// <summary>
        /// 是否显示提示内容框
        /// </summary>
        private string? _oprateResShow = "Collapsed";

        public string? OprateResShow
        {
            get { return _oprateResShow; }
            set { SetProperty(ref _oprateResShow, value); }
        }

        /// <summary>
        /// 保存操作结果
        /// </summary>
        private string? _operateRes;

        public string? OperateRes
        {
            get { return _operateRes; }
            set
            {
                if (value == "保存成功!")
                {
                    OperateResColor = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    OperateResColor = new SolidColorBrush(Colors.Red);
                }
                SetProperty(ref _operateRes, value);
            }
        }

        /// <summary>
        /// 操作结果显示的颜色
        /// </summary>
        private SolidColorBrush? _operateResColor;

        public SolidColorBrush? OperateResColor
        {
            get { return _operateResColor; }
            set { SetProperty(ref _operateResColor, value); }
        }


    }
}
